// 初始化
let image = $('#image')

let option = {
  aspectRatio: 1,
  preview:'.img-preview'
}

image.cropper(option)
// 单击事件
$("button:contains('上传')").on('click',function () {
  $('#file').trigger('click')
})
// 
$('#file').on('change',function () {
  if (this.files.length > 0) {
    let fileObj = this.files[0]
    let url = URL.createObjectURL(fileObj)
    image.cropper('replace',url)
  }
})
// 
$("button:contains('确定')").on('click',function () {
 let canvas = image.cropper('getCroppedCanvas',{width:30,height:30})
 let base64 = canvas.toDataURL()
 $.ajax({
   type:'post',
   url:'/my/user/avatar',
   data:{avatar:base64},
   success:function (res) {
     if (res.status === 0) {
       layer.msg(res.message)
       window.parent.getUserInfo()
     }
   }
 })
})